package com.example.content.activity

import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import androidx.annotation.ColorRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.example.content.R
import com.gyf.immersionbar.ImmersionBar
import com.hyphenate.easeui.utils.StatusBarCompat
import com.share.um.ShareHelper

/**
 * Created by zhudongao
 * Desc
 * Date 2020/10/25
 */
open class BaseUIActivity : AppCompatActivity()  {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ImmersionBar.with(this).statusBarDarkFont(true).init()
    }

    protected open fun initSystemFit() {
        setFitSystemForTheme(true)
    }

    /**
     * 通用页面，需要设置沉浸式
     * @param fitSystemForTheme
     */
    open fun setFitSystemForTheme(fitSystemForTheme: Boolean) {
        setFitSystemForTheme(fitSystemForTheme, R.color.white)
        setStatusBarTextColor(false)
    }

    /**
     * 设置是否是沉浸式，并可设置状态栏颜色
     * @param fitSystemForTheme
     * @param colorId 颜色资源路径
     */
    open fun setFitSystemForTheme(fitSystemForTheme: Boolean, @ColorRes colorId: Int) {
        setFitSystem(fitSystemForTheme)
        //初始设置
        StatusBarCompat.compat(this, ContextCompat.getColor(this, colorId))
    }

    /**
     * 修改状态栏文字颜色
     * @param isLight 是否是浅色字体
     */
    open fun setStatusBarTextColor(isLight: Boolean) {
        StatusBarCompat.setLightStatusBar(this, !isLight)
    }

    /**
     * 设置是否是沉浸式
     * @param fitSystemForTheme
     */
    open fun setFitSystem(fitSystemForTheme: Boolean) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
        if (fitSystemForTheme) {
            val contentFrameLayout = findViewById<View>(Window.ID_ANDROID_CONTENT) as ViewGroup
            val parentView = contentFrameLayout.getChildAt(0)
            if (parentView != null && Build.VERSION.SDK_INT >= 14) {
                parentView.fitsSystemWindows = true
            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window = window
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        }
    }
}